import sys, os, shutil, re

def errorinfo(tips):
    print "ERROR:commit message doesn't match standard format,please check!"
    print "======standard format ========="
    print "module: change summary"
    print ""
    print "[Require/DTS]:DTS[0-9]{13}$/S[0-9]"
    print "[Changelog]:..."
    print "[Author]:..."
    print "============================"
    print tips
    sys.exit(1)

def msg_check(msg):
    summary = msg.split("\n")

    #check revert msg
    list = re.findall('^\s*Revert*',summary[0])
    if 1 == len(list):
        if 5 == len(summary):
            sys.exit(0)
    
    #check commit msg or cherrypick msg
    if ":" not in summary[0]:
        errorinfo("some error on module: change summary")
    if "module" in summary[0] and "change summary" in summary[0]:
        print "Commit failed."
        print "Error No.10: Please change the summary, you shoudn't just use the template"
        sys.exit(1)
    #need space line before [Require/DTS]:
    list = re.findall('\s+$',summary[1])
    if 1 > len(list) and "" != summary[1]:
        errorinfo("Error No.11: There must be a space line before '[Require/DTS]:'")
    if "[Require/DTS]" in summary[2]:
        ticket = summary[2].split("[Require/DTS]:")[1].replace(" ","")
        list = re.findall('^\s*S[0-9]|^\s*DTS[0-9]{13}$',ticket)
        if 1 > len(list):
            errorinfo("Error No.12: Some error on [Require/DTS]:DTS[0-9]{13}$/S[0-9]")
    else:
        errorinfo("Error No.12: Some error on [Require/DTS]:DTS[0-9]{13}$/S[0-9]")
    if "[Changelog]:" in msg:
        log = msg.split("[Changelog]:")[1].split("\n")[0]
        list = re.findall('.*\w+',log)
        if 1 > len(list):
            errorinfo("Error No.13: Some error on [Changelog]:.*\w+")
    else:
        errorinfo("Error No.13: Some error on [Changelog]:.*\w+")
    if "[Author]:" in msg:
        name = msg.split("[Author]:")[1].split("\n")[0]
        list = re.findall('.*\w+',name)
        if 1 > len(list):
            errorinfo("Error No.14: Some error on [Author]:.*\w+")
    else:
        errorinfo("Error No.14: Some error on [Author]:.*\w+")

if __name__ == "__main__":
    debug = 1
    if debug == 1:
        file_object = open('a', 'r')
        msg = file_object.read()
        file_object.close( )
    else:
        msg = sys.argv[1]
    #print msg
    msg_check(msg)
    sys.exit()
    
